// querySeatHours.js

import axios from "axios";
import dayjs from "dayjs";

// const declarationRemark = "见习夜班申报";

export default function (dateRange) {
  let start =
    dayjs(dateRange[0]).subtract(1, "day").format("YYYY-MM-DD") + " 00:00:00";
  let end =
    dayjs(dateRange[1]).add(1, "day").format("YYYY-MM-DD") + " 23:59:59";
  let payload1 = { start, end };
  let payload2 = {
    method: "GET",
    startTime: start,
    endTime: end,
    // filters: [{ field: "remark", value: declarationRemark }],
  };
  console.log("querySeatHours", payload1, payload2);
  return new Promise((resolve, reject) => {
    axios
      .post("api/seat_hours_get.php", payload1)
      .then((response) => {
        let result = {
          start: response.data.start,
          end: response.data.end,
          count: response.data.count,
          data: response.data.content,
          users: response.data.users,
          declarations: [],
          mixture: [],
        };
        // 查找declaration
        axios
          .post("api/seat_hours_declaration.php", payload2)
          .then((response2) => {
            let declarations = response2.data.content.map((cell) => {
              // 申报数据添加 type=1
              cell["type"] = 1;
              return cell;
            });
            result.declarations = declarations;
            let rawData = result.data.map((cell) => {
              // 一般数据添加 type=0
              cell["asHour"] = 1;
              cell["type"] = 0;
              return cell;
            });
            let mixture = [...rawData, ...declarations];
            mixture.sort((a, b) => a.startTime - b.startTime);
            result.mixture = mixture;
            console.log("querySeatHours", response2.data);
            console.log("querySeatHours", result);
            resolve(result);
          })
          .catch((error) => {
            reject(error);
          });
      })
      .catch((error) => {
        reject(error);
      });
  });
}
